<?php
namespace app\librarys;


class Fanso2oInteface
{
    //自定义函数 Virtual coupons
    //组装卡片列表返回数据
    public static function helper_goodsListToResult($goodsList,$pageNo,$pageSize,$totalPage){
        return [
            'goodsList'=>$goodsList,
            'pageNo'=>$pageNo,
            'pageSize'=>$pageSize,
            'totalPage'=>$totalPage,
        ];
    }
    ///通过站点列表获取卡券列表
    public static function helper_getGoodsListByShopList($shopList){
        $shopIdList=[];
        foreach($shopList as $item){
            $shopIdList[]=$item->shopId;
        }
//        var_dump($shopIdList);exit;
        return self::helper_getGoodsListByShopIdList($shopIdList);
    }
    public static function helper_getGoodsListByShopIdList($shopIdList){
        //获取活动列表
        $activityList=[];
        foreach($shopIdList as $item){
            try {
                $activityList[]=[
                    'shopId'=>$item,
                    'info'=>Fanso2oInteface::getLatestActivity($item),
                ];
            }catch(\Exception $e){
                //忽略没有有效活动的站点--------页面和分页数字不对的原因
            }
        }
//        echo "活动列表:<br>";
//        echo Tools::var_dump_out($activityList);exit;
        //产品列表
        $goodsList=[];
        foreach($activityList as $item){
            $goods['amount']=$item['info']->amount;
            $goods['shopId']=$item['shopId'];
            $goods['info']=Fanso2oInteface::activityResource($item['info']->activityId)[0];
            $goodsList[]=$goods;
        }
//        echo "产品列表<br/>";
//        echo Tools::var_dump_out($goodsList);exit;
        return $goodsList;
    }
    //获取所有卡券
    public static function helper_getAllGoodsList($pageNo=1,$pageSize=20){
        //站点列表
        $shopList=Fanso2oInteface::queryShopList($pageNo,$pageSize);
//        var_dump($shopList);exit;
        $goodsList=self::helper_getGoodsListByShopList($shopList->results);
        $result=Fanso2oInteface::helper_goodsListToResult($goodsList,$shopList->pageNo,$shopList->pageSize,$shopList->totalPage);
        return $result;
    }
    //通过分类名称获取某个分类的所有卡券
    public static function helper_getCategoryGoodsList($tagName,$pageNo=1,$pageSize=20){
        $tagList1=self::queryTagOfShop();
        $tagList2=self::getCustomTag(1);
        $tagList=array_merge($tagList1,$tagList2);
//        var_dump($tagList1);var_dump($tagList2);var_dump($tagList) ; exit;
        $tagId=false;
        foreach($tagList as $item){
            if($tagName==$item->tagName){
                $tagId=  $item->tagId;
                break;
            }
        }
        if($tagId){
            //站点列表
            $shopList=Fanso2oInteface::queryShopByTag($tagId,$pageNo,$pageSize);
//            var_dump($shopList);exit;
            $goodsList=self::helper_getGoodsListByShopList($shopList->results);
            $result=Fanso2oInteface::helper_goodsListToResult($goodsList,$shopList->pageNo,$shopList->pageSize,$shopList->totalPage);
            return $result;
        }
       return Fanso2oInteface::helper_goodsListToResult([],0,$pageSize,0);
    }



    //api
    //查询网点列表
    //type0所有  1有效的活动
    public static function queryShopList( $pageNo=1,$pageSize=20,$type=1)
    {
        $data = ['pageSize' => $pageSize, 'pageNo' => $pageNo,'type'=>$type];
        return self::_getResponseData('/api/company/queryShopList', $data);
    }
    //查询网点下活动
    public static function getLatestActivity($shopId)
    {
        $data = ['shopId' => $shopId];
        return self::_getResponseData('/api/activity/getLatestActivity', $data);
    }
    //获取某个活动关联的红包
    public static function activityResource($activityId)
    {
        $data = ['activityId' => $activityId];
        return self::_getResponseData('/api/activity/activityResource', $data);
    }
    //热门标签
    public static function getTags($type)
    {
        $data = ['type' => $type];
        return self::_getResponseData('/api/category/getTags', $data);
    }
    public static function getCustomTag($type)
    {
        $data = ['type' => $type];
        return self::_getResponseData('/api/category/getCustomTag', $data);
    }
    //获取活动的详细信息
    public static function activityInfo($activityId)
    {
        //amount
        $data = ['activityId' => $activityId];
        return self::_getResponseData('/api/activity/activityInfo', $data);
    }
    //查询已使用过的网点标签API
    public static function queryTagOfShop()
    {
        $data = [];
        return self::_getResponseData('/api/company/queryTagOfShop', $data);
    }
    //通过标签查询商家网点API
    public static function queryShopByTag($tagId,$pageNo=1,$pageSize=20,$type=1)
    {
        $data = [
            'tagId'=>$tagId,
            'pageNo' => $pageNo,
            'pageSize' => $pageSize,
            'type'=>$type,
        ];
        return self::_getResponseData('/api/company/queryShopByTag', $data);
    }

    //我的红包库链接  http://m.fanso2o.com/home/index2?v=3
    //领取链接 http://m.fanso2o.com/shop/ccc1b46290c849b98c3fa3aec2b39264
    //直接领取，注意选对应模板








    private static function _getResponseData($apiName, $data)
    {
        $response = self::_getResponse($apiName, $data);

        if ($response->status == 0) {
            return $response->data;
        } else {
            throw new \Exception($apiName.'--'.$response->msg, $response->status);
        }
    }

    private static function _getResponse($apiName, $data)
    {
        $appKey = self::_getAppKey();
        $appSecret = self::_getAppSecret();
        $timestamp = time();
        $signature = Tools::desEncode(strtoupper(md5($appKey . $timestamp)), $appSecret, '52136585');

        $postData = [
            'appKey' => $appKey,
            'signature' => $signature,
            'timestamp' => $timestamp
        ];

        if ($data != null) {
            foreach ($data as $key => $value) {
                $postData[$key] = $value;
            }
        }
        $responseString = Tools::postForm(self::_getRequestUrl($apiName), $postData);
        $response = json_decode($responseString);
        return $response;
    }

    private static function _getRequestUrl($apiName)
    {
        $address = self::_getAddress();
        $url = $address . $apiName;

        return $url;
    }

    private static function _getAddress()
    {
        return \Yii::$app->params['fanso2o.inteface.address'];
    }

    private static function _getAppKey()
    {
        return \Yii::$app->params['fanso2o.inteface.appKey'];
    }

    private static function _getAppSecret()
    {
        return \Yii::$app->params['fanso2o.inteface.appSecret'];
    }
}